package alpha.com.google.code.mechanic.janitor.ui;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.CheckboxTableViewer;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * The class is the wizard page that shows the changed Eclipse preferences and
 * let users indicate whether save the preferences to a new Task.
 * 
 * @author jingjiejiang
 * @since Oct 5, 2014
 */
public class SavedPrefsWizardPage extends WizardPage {
  
  private Table tableSavedPrefs;
  private CheckboxTableViewer chkTblSavedPrefs;
  private Button btnSaveToNewTask;
  private Button btnSaveToCurTask;
  
  // The field is to indicate whether save the new preferences to a new Task. 
  private boolean isSaveToNewTask;
  // The field represents the new preferences.
  private Map<String, String> savedPrefs;

  /**
   * Create the wizard.
   */
  public SavedPrefsWizardPage(boolean isSaveToNewTask, Map<String, String> preferences) {
    super("wizardPage");
    
    setTitle("Preference Recorder Output");
    setDescription("Save new preferences.");
    
    this.isSaveToNewTask = isSaveToNewTask;
    savedPrefs = preferences;
  }
  
  public boolean isSaveToNewTask() {
    return isSaveToNewTask;
  }

  public void setSaveToNewTask(boolean isSaveToNewTask) {
    this.isSaveToNewTask = isSaveToNewTask;
  }

  /**
   * Create contents of the wizard.
   * @param parent
   */
  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);

    setControl(container);
    
    Label lblNewLabel = new Label(container, SWT.NONE);
    lblNewLabel.setBounds(10, 0, 110, 14);
    lblNewLabel.setText("Saved Preferences:");
    
    chkTblSavedPrefs = CheckboxTableViewer.newCheckList(container, SWT.BORDER | SWT.FULL_SELECTION);
    tableSavedPrefs = chkTblSavedPrefs.getTable();
    tableSavedPrefs.setLinesVisible(true);
    tableSavedPrefs.setHeaderVisible(true);
    tableSavedPrefs.setBounds(10, 20, 570, 193);
    
    TableViewerColumn tblClmnTaskKey = new TableViewerColumn(chkTblSavedPrefs, SWT.NONE);
    TableColumn clmnPrefKey = tblClmnTaskKey.getColumn();
    clmnPrefKey.setWidth(250);
    clmnPrefKey.setText("      Key");
    
    TableViewerColumn tblClmnTaskValue = new TableViewerColumn(chkTblSavedPrefs, SWT.NONE);
    TableColumn tblclmnValue = tblClmnTaskValue.getColumn();
    tblclmnValue.setWidth(300);
    tblclmnValue.setText("Value");
    
    chkTblSavedPrefs.setLabelProvider(new SavedPrefsTableLabelProvider());
    chkTblSavedPrefs.setContentProvider(new ArrayContentProvider());
    chkTblSavedPrefs.setInput(savedPrefs.keySet().toArray());
    chkTblSavedPrefs.setAllChecked(true);
    
    initBtnSaveToCurTask(container);
    initBtnSaveToNewTask(container);
    
    Button btnSelAll = new Button(container, SWT.NONE);
    btnSelAll.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        chkTblSavedPrefs.setAllChecked(true);
      }
    });
    btnSelAll.setBounds(10, 219, 94, 28);
    btnSelAll.setText("Select All");
    
    Button btnDeselAll = new Button(container, SWT.NONE);
    btnDeselAll.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        chkTblSavedPrefs.setAllChecked(false);
      }
    });
    btnDeselAll.setBounds(105, 219, 94, 28);
    btnDeselAll.setText("Deselect All");
  }
  
  private void initBtnSaveToCurTask (Composite composite) {
    
    btnSaveToCurTask = new Button(composite, SWT.RADIO);
    btnSaveToCurTask.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        
        isSaveToNewTask = false;
        btnSaveToCurTask.setSelection(true);
        btnSaveToNewTask.setSelection(false);
      }
    });
    btnSaveToCurTask.setBounds(10, 248, 469, 18);
    btnSaveToCurTask.setText("Insert the saved preferences into an existing task of the current activated Context");
    btnSaveToCurTask.setSelection(false);
  }
  
  private void initBtnSaveToNewTask (Composite composite) {
    
    btnSaveToNewTask = new Button(composite, SWT.RADIO);
    btnSaveToNewTask.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        
        isSaveToNewTask = true;
        btnSaveToCurTask.setSelection(false);
        btnSaveToNewTask.setSelection(true);
      }
    });
    btnSaveToNewTask.setBounds(10, 272, 283, 18);
    btnSaveToNewTask.setText("Create a new Task for the saved preferences");
    btnSaveToNewTask.setSelection(true);
  }
  
  private class SavedPrefsTableLabelProvider extends LabelProvider
      implements ITableLabelProvider {

    public Image getColumnImage(Object element, int columnIndex) {
      return null;
    }

    public String getColumnText(Object element, int columnIndex) {
      
      String key = (String) element;
      switch (columnIndex) {
      case 0:
        return key;
        
      case 1:
        String value = savedPrefs.get(key);
        return value != null ? value : "(null)";
        
      default:
        return null;
      }
    }
  }

  public Map<String, String> getSelectedPrefs () {
    
    Map<String, String> selpref = new HashMap<String, String>();
    
    Object[] selkeys = chkTblSavedPrefs.getCheckedElements();
   
    for (Object key: selkeys) {
      
      String keyid = (String) key;
      
      if (savedPrefs.containsKey(keyid)) {
        selpref.put(keyid, savedPrefs.get(keyid)); 
      }
    }
    
    return selpref;
  }
  
}
